/* eslint-disable */
const tagsView = {
  state: {
    visitedViews: [],
    cachedViews: [],
    keepAliveRoute: (sessionStorage.getItem('keepAliveRoute') && sessionStorage.getItem('keepAliveRoute').split(',')) || []},
  mutations: {
    ADD_VISITED_VIEWS: (state, view) => {
      for(let i=0;i<state.visitedViews.length;i++){
        if(view.path === state.visitedViews[i].path){
          state.visitedViews[i] = Object.assign({}, view, {title: view.path})
          return false
        }
      }
      state.visitedViews.push(
        Object.assign({}, view, {
          title: view.path
        })
      )
      state.cachedViews.push(view.name)
    },
    DEL_VISITED_VIEWS: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews.splice(i, 1)
          break
        }
      }
      for (const i of state.cachedViews) {
        if (i === view.name) {
          const index = state.cachedViews.indexOf(i)
          state.cachedViews.splice(index, 1)
          break
        }
      }
    },
    DEL_OTHERS_VIEWS: (state, view) => {
      for (const [i, v] of state.visitedViews.entries()) {
        if (v.path === view.path) {
          state.visitedViews = state.visitedViews.slice(i, i + 1)
          break
        }
      }
      for (const i of state.cachedViews) {
        if (i === view.name) {
          const index = state.cachedViews.indexOf(i)
          state.cachedViews = state.cachedViews.slice(index, i + 1)
          break
        }
      }
    },
    DEL_ALL_VIEWS: state => {
      state.visitedViews = []
      state.cachedViews = []
    },
    SET_KEEP_ALIVE_ROUTE: (state, view) => {
      const keepAliveRoute = state.keepAliveRoute
      if (keepAliveRoute.length === 0) {
        keepAliveRoute.push(view)
      } else {
        let flag = false
        for (const i in keepAliveRoute) {
          if (keepAliveRoute[i] === view) {
            flag = true
            break
          }
        }
        if (!flag) {
          keepAliveRoute.push(view)
        }
      }
      sessionStorage.setItem('keepAliveRoute', keepAliveRoute.join(','))
    },
    DEL_KEEP_ALIVE_ROUTE: (state, view) => {
      const keepAliveRoute = state.keepAliveRoute
      for (const i in keepAliveRoute) {
        if (keepAliveRoute[i] === view) {
          keepAliveRoute.splice(i, 1)
          break
        }
      }
      sessionStorage.setItem('keepAliveRoute', keepAliveRoute.join(','))
    }
  },
  actions: {
    addVisitedViews({ commit }, view) {
      commit('ADD_VISITED_VIEWS', view)
    },
    delVisitedViews({ commit, state }, view) {
      return new Promise(resolve => {
        commit('DEL_VISITED_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delOthersViews({ commit, state }, view) {
      return new Promise(resolve => {
        commit('DEL_OTHERS_VIEWS', view)
        resolve([...state.visitedViews])
      })
    },
    delAllViews({ commit, state }) {
      return new Promise(resolve => {
        commit('DEL_ALL_VIEWS')
        resolve([...state.visitedViews])
      })
    },
    setKeepAliveRoute({ commit, state }, view) {
      commit('SET_KEEP_ALIVE_ROUTE', view)
    },
    delKeepAliveRoute({ commit, state }, view) {
      commit('DEL_KEEP_ALIVE_ROUTE', view)
    }
  }
}

export default tagsView
